import _ from 'lodash';
import i18n from '@src/locales'

/** 
 * @description 根据工时记录设置确定最终自定义字段
 */
function packFields(fields, config) {
  let _fields = _.cloneDeep(fields);
  let newFields = [];

  _fields.forEach(field => {

    // 备注字段前增加行程距离
    if (field.formType == 'text') {
      newFields.push({
        displayName: `${i18n.t('task.list.strokeDistance')} (KM)`,
        fieldName: 'distance',
        formType: 'number',
        enabled: config.distanceStatis ? 1 : 0
      }, {
        ...field
      }, )
    }

    // 在开始时间，结速时间后加位置
    else if (field.formType == 'datetime') {
      if (field.fieldName == 'startTime' && config.autoLocation) {
        newFields.push({
          ...field,
          minWidth: '160px',
        }, {
          displayName: i18n.t('common.form.type.location'),
          fieldName: 'startAddress',
          formType: 'number',
          enabled: config.distanceStatis ? 1 : 0,
          showTooltip:true
        })

      } else {
        newFields.push({
          ...field,
          minWidth: '160px',
        }, {
          displayName: i18n.t('common.form.type.location'),
          fieldName: 'endAddress',
          formType: 'number',
          enabled: config.distanceStatis ? 1 : 0,
          showTooltip:true
        })
      }

    }
    // 用时时间（H）
    else if (field.fieldName === 'usedTime') {
      field.displayName = `${field.displayName}(H)`;
    } else {
      // 过滤掉附件
      if(field.formType !== 'attachment'){
        newFields.push(field);
      }
    }
  })

  return newFields;
}

/** 
 * @description 将工时记录自定义字段转成列表所需显示
 */
function toTableFields(fields, config) {

  let tableFields = packFields(fields, config);

  return [
    ...tableFields,
    {
      displayName: i18n.t('task.list.operator'),
      fieldName: 'operatorName',
      minWidth: '80px',
      enabled: 1
    },
    {
      displayName: i18n.t('task.list.operateTime'),
      fieldName: 'operateTime',
      minWidth: '160px',
      enabled: 1
    }
  ];
}

/** 
 * @description 自定义字段特殊处理
 */
function packCustomFields(fields) {
  let formTypes = ['autograph', 'attachment', 'separator', 'info'];
  let newCard = fields.filter(field=>{
    if(field.formType !== 'attachment'){
      if(field.formType == 'datetime'){
        field.minWidth = '160px' ;
      }
      if(field.formType == 'related_task'){
        field.minWidth = '160px' ;
      }
      if(field.formType == 'address' || field.formType == 'location'){
        field.minWidth = '160px' ;
      }
               
      return !field.isHidden && (field.isVisible == undefined || field.isVisible) && formTypes.indexOf(field.formType) < 0
    }
  }) 
  return [
    {
      displayName: i18n.t('common.form.type.eventNo'),
      fieldName: 'eventNo',
      minWidth: '150px',
      enabled: 1
    },
    {
      displayName: i18n.t('common.form.type.customerName'),
      fieldName: 'cusName',
      enabled: 1
    },
    {
      displayName: i18n.t('common.base.contact'),
      fieldName: 'lmName',
      minWidth: '150px',
      enabled: 1
    },
    ...newCard, 
    {
      displayName: i18n.t('common.event.eventState'),
      fieldName: 'state',
      enabled: 1
    },
    {
      displayName: i18n.t('common.event.eventType'),
      fieldName: 'templateName',
      enabled: 1
    },
    {
      displayName: i18n.t('task.list.operator'),
      fieldName: 'userName',
      minWidth: '80px',
      enabled: 1
    },
    {
      displayName: i18n.t('task.list.operateTime'),
      fieldName: 'updateTime',
      minWidth: '160px',
      enabled: 1
    }]
}

/** 
 * @description 自定义产品客户字段特殊处理
 */
function packProductFields(fields) {
  let formTypes = ['autograph', 'attachment', 'separator', 'info'];
  let newCard = fields.filter(field=>{
    if(field.formType !== 'attachment'){
      if(field.formType == 'datetime'){
        field.minWidth = '160px' ;
      }
      if(field.formType == 'related_task'){
        field.minWidth = '160px' ;
      }
      if(field.formType == 'address' || field.formType == 'location'){
        field.minWidth = '160px' ;
      }
               
      return !field.isHidden && (field.isVisible == undefined || field.isVisible) && formTypes.indexOf(field.formType) < 0
    }
  }) 
  return [ 
    ...newCard
  ]
}

/** 
 * @description 备件服务清单列表展示列
 */
function packSpareFields(fields, packSpareFields) { 
  let systemFields = []
  if(packSpareFields == 'sparePartInventory'){// 备件清单系统字段
    systemFields.push({
      displayName: i18n.t('task.detail.components.appliedNum'),
      fieldName: 'applySumNum',
      minWidth: '100px',
      enabled: 1
    })
  }
  let newCardFields = fields.filter(field=> field.enabled == 1 && field.isHidden == 0)
  return [ 
    {
      displayName: i18n.t('common.form.type.taskNo'),
      fieldName: 'taskNo',
      minWidth: '120px',
      enabled: 1
    },
    {
      displayName: i18n.t('common.task.taskType'),
      fieldName: 'taskType',
      minWidth: '100px',
      enabled: 1
    }, {
      displayName: i18n.t('common.form.type.customerName'),
      fieldName: 'customerName',
      minWidth: '100px',
      enabled: 1
    },
    {
      displayName: i18n.t('common.base.contact'),
      fieldName: 'taskLinkManName',
      minWidth: '100px',
      enabled: 1
    },
    {
      displayName: i18n.t('common.base.contactPhone'),
      fieldName: 'taskLinkManPhone',
      minWidth: '120px',
      enabled: 1
    }, {
      displayName: i18n.t('task.executor'),
      fieldName: 'executorName',
      minWidth: '80px',
      enabled: 1
    }, {
      displayName: i18n.t('task.synergies'),
      fieldName: 'synergies',
      minWidth: '100px',
      enabled: 1
    },
    ...newCardFields, ...systemFields ]
}

const fieldUtil = {
  packFields,
  toTableFields,
  packCustomFields,
  packProductFields,
  packSpareFields
}

export default fieldUtil;